package com.bazinga.contact.view;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.bazinga.contact.model.AllCreatedJobs;
import com.bazinga.contact.model.SingleCreatedJob;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CreatedJobsAction_OLD extends ActionSupport implements SessionAware{


	private static final long serialVersionUID = 5650923110652312524L;
	
	private final String dbServer = "127.0.0.1";
	private Map<String, Object> session;
	
	private String userid = null;
	private String jobid = null;
	private String title = null;
	private String salary = null;

	private AllCreatedJobs allJobs; 
	
	public CreatedJobsAction_OLD(){
		setAllJobs(new AllCreatedJobs());
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
		
	}

	public String execute() {
		this.session = ActionContext.getContext().getSession();
		if(session.containsKey("userid")) {
			userid = (String) session.get(userid);
		}
		
		String dbUrl = "jdbc:mysql://" + dbServer + ":3306/";
		String dbName = "bazinga";
		String driverName = "org.gjt.mm.mysql.Driver";
		String username = "root";
		String password = "";
		ResultSet rs = null;
		ResultSet rs2 = null;
		ResultSet rs3 = null;
		
		Connection conn = null;
		Statement stmt = null;
		
		String studentname = null;
		String studentid = null;
		
		String searchQuery = "select jobid,title,salary from jobdetails where userid='" + userid + "' ;";
		try
		{
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dbUrl + dbName, username, password);
			stmt = conn.createStatement();
		}
		catch (Exception e) {
			System.out.println(e.toString());
		}
		try {
			rs = stmt.executeQuery(searchQuery);
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
			return "error";
		}
		
		try {
			while (rs.next()) {
				
				jobid = rs.getString("jobid");	
				setTitle(rs.getString("title"));	
				setSalary(rs.getString("salary"));	
				
				SingleCreatedJob singleJob = new SingleCreatedJob(jobid);
				List<String> students = new ArrayList<String>();
				try {
					String newQuery = "select userid from applications where jobid='" + jobid + "' ;";
					rs2 = stmt.executeQuery(newQuery);
				}
				catch (SQLException e) {
					e.printStackTrace();
					return "error";
				}
				
				try {
					while (rs2.next()) {
						
						studentid = rs2.getString("userid");
						String newQuery2 = "select fname,lname from userDetails where userid='" + studentid+ "' ;";
						rs3 = stmt.executeQuery(newQuery2);
						
					}
					
				} catch (SQLException e) {
					e.printStackTrace();
					return "error";
				}
				
				try {
					
						while (rs3.next()) {
							studentname = rs3.getString("fname") + " " + rs3.getString("lname");
							students.add(studentname);			
						}
				}
				catch (SQLException e) {
					e.printStackTrace();
					return "error";
				}
				
				singleJob.setUserList(students);
				allJobs.addJob(singleJob);
			}

		}
		
		catch(SQLException ex) {
			ex.printStackTrace();
			return "error";
		}
		
		
		return "success";
	}

	public AllCreatedJobs getAllJobs() {
		return allJobs;
	}

	public void setAllJobs(AllCreatedJobs allJobs) {
		this.allJobs = allJobs;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSalary() {
		return salary;
	}

	public void setSalary(String salary) {
		this.salary = salary;
	}


	

}
